﻿using System;
using System.Linq;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using TSB.SPEventReceiverManager.Entities;

namespace TSB.SPEventReceiverManager.Layouts.TSBLayouts
{
    public partial class ManageEventReceivers : LayoutsPageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            SetBackLink();
            SetPanelVisibility(Panels.HideAll);

            if (Context.Request.QueryString["listId"] == null)
            {
                SetPanelVisibility(Panels.NoList);
                return;
            }

            var listId = Guid.Empty;
            try
            {
                listId = new Guid(Context.Request.QueryString["listId"]);
            }
            catch
            {
                SetPanelVisibility(Panels.NoList);
                return;
            }

            GetListDetails(listId);

        }

        void GetListDetails(Guid listId)
        {
            var spList = SPContext.Current.Web.Lists[listId];

            if (spList == null)
            {
                ltListDetails.Text = "List not found";
                return;
            }

            ltListDetails.Text = ltListDetails.Text.Replace("{title}", spList.Title).Replace("{url}", spList.ParentWebUrl);

            if (spList.EventReceivers == null || spList.EventReceivers.Count == 0)
            {
                SetPanelVisibility(Panels.NoResults);
                return;
            }

            var eventReceivers = (from SPEventReceiverDefinition eventReceiver in spList.EventReceivers
                                  select new EventReceiver
                                  {
                                      Name = eventReceiver.Name,
                                      Assembly = eventReceiver.Assembly,
                                      Class = eventReceiver.Class,
                                      Type = eventReceiver.Type.ToString()
                                  }).ToList();

            rptReceivers.DataSource = eventReceivers.OrderBy(x => x.Name).ToList();
            rptReceivers.ItemDataBound += rptReceivers_ItemDataBound;
            rptReceivers.DataBind();
            SetPanelVisibility(Panels.Results);
        }

        void rptReceivers_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType != ListItemType.Item && e.Item.ItemType != ListItemType.AlternatingItem)
            {
                return;
            }

            var lt = e.Item.Controls[0] as LiteralControl;
            var ev = e.Item.DataItem as EventReceiver;

            if (lt == null || ev == null)
            {
                return;
            }

            lt.Text =
                lt.Text.Replace("{name}", ev.Name).Replace("{assembly}", ev.Assembly).Replace("{class}", ev.Class).
                    Replace("{type}", ev.Type);
        }

        void SetPanelVisibility(Panels panel)
        {
            rptReceivers.Visible = panel == Panels.Results;
            ltNoResults.Visible = panel == Panels.NoResults;
            ltNoList.Visible = panel == Panels.NoList;

            ltListDetails.Visible = panel == Panels.NoResults || panel == Panels.Results;
        }

        void SetBackLink()
        {
            lnkBack.NavigateUrl = string.Format("{0}/_layouts/listedit.aspx?List={1}", SPContext.Current.Web.Url, Context.Request.QueryString["listId"]);
        }

        enum Panels
        {
            Results, NoList, NoResults, HideAll
        }
    }
}
